<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/25
 * Time: 20:08
 */

namespace app\common\controller;


use app\common\model\Shop;
use app\common\model\SpecGoodsPrice;
use app\common\model\Goods;
use app\common\utils\JsonService;
use app\common\model\Cart as CartModel;
use think\response\Json;

class Cart extends BaseController
{
    public $member_id = 0;
    public $shop = null;
    public $shop_id = 0;
    public $member = 0;

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        if(!session('member_id')){
            //$this->redirect(url('index/login/login',['action'=>'seller_login'])->suffix(false)->build());
            return JsonService::error('请登录后操作',-1);
        }
        $this->member_id = session('member_id');
        $this->member = session('memberInfo');
        //$this->assign('member_id',$this->member_id);
        //$this->assign('member',$this->member);

        $shop_admin = Shop::getOne(['member_id'=>$this->member_id]);
        $this->shop = $shop_admin;
        $this->shop_id = $shop_admin['shop_id'];
        //$this->assign('shop_id',$this->shop_id);
        //$this->assign('shop_admin',$shop_admin);
    }

    public function addCart(){
        if($this->request->isAjax()){
            if(!$this->member_id){
                return JsonService::error('请登录后操作',-1);
            }
            $post = $this->request->post();
            $goods_id = $post['goods_id'];
            $spec_item_id = $post['spec_item_id'];
            $goods_num = $post['goods_num'];
            $goods = Goods::getOne(['goods_id'=>$goods_id]);
            if($goods['store_count'] < $goods_num){
                return JsonService::error('商品库存不足');
            }
            $shop_price = $goods['shop_price'];
            $spec = [];
            if($spec_item_id >0){
                $spec = SpecGoodsPrice::getOne(['item_id'=>$spec_item_id,'goods_id'=>$goods_id]);
                if($spec['store_count'] < $goods_num){
                    return JsonService::error('商品库存不足');
                }
                $shop_price = $spec['price'];
            }

            $cartGoods = CartModel::getOne(['user_id'=>$this->member_id,'goods_id'=>$goods_id,'spec_price_goods_id'=>$spec_item_id,'shop_id'=>$this->shop_id],'id');
            $insert = [
                'user_id'=>$this->member_id,
                'shop_id'=>$goods['shop_id'],
                'goods_id'=>$goods_id,
                'goods_name'=>$goods['goods_name'],
                'market_price'=>$goods['market_price'],
                'goods_price'=>$shop_price,
                'goods_num'=>$goods_num,
                'spec_price_goods_id'=>$spec_item_id,
                'spec_key'=>isset($spec['key'])?$spec['key']:'',
                'spec_key_name'=>isset($spec['key_name'])?$spec['key_name']:'',
                'add_time'=>time()
            ];
            if(empty($cartGoods)){
                CartModel::create($insert);
            }else{
                CartModel::where(['id'=>$cartGoods['id']])->inc('goods_num',$goods_num)->update();
            }
            return JsonService::success('加入成功');
        }
    }

    //购物车加减操作
    public function updateCartGoods(){
        if($this->request->isAjax()){
            $cart_id = $this->request->post('cart_id');
            $num = 1;
            $action = $this->request->post('action','plus');
            $cartGoods = CartModel::where(['id'=>$cart_id,'user_id'=>$this->member_id])->with(['getGoods'])->find();
            //判断是否是规格产品
            if($action == 'plus') {
                if ($cartGoods['spec_price_goods_id'] > 0) {
                    $specGoods = SpecGoodsPrice::getOne(['item_id' => $cartGoods['spec_price_goods_id'], 'goods_id' => $cartGoods['goods_id'], 'key' => $cartGoods['spec_key']], 'store_count');
                    if ($cartGoods['goods_num'] + $num > $specGoods['store_count']) {
                        return JsonService::error('数量超出库存数量');
                    }
                } else {
                    if ($cartGoods['goods_num'] + $num > $cartGoods['getGoods']['store_count']) {
                        return JsonService::error('数量超出库存数量');
                    }
                }
                CartModel::where(['id'=>$cart_id,'user_id'=>$this->member_id])->inc('goods_num',1)->update();
            }else{
                if ($cartGoods['spec_price_goods_id'] > 0) {
                    $specGoods = SpecGoodsPrice::getOne(['item_id' => $cartGoods['spec_price_goods_id'], 'goods_id' => $cartGoods['goods_id'], 'key' => $cartGoods['spec_key']], 'store_count');
                    if ($cartGoods['goods_num'] - $num <1) {
                        return JsonService::error('数量不能小于1');
                    }
                } else {
                    if ($cartGoods['goods_num'] - $num <1) {
                        return JsonService::error('数量不能小于1');
                    }
                }
                CartModel::where(['id'=>$cart_id,'user_id'=>$this->member_id])->dec('goods_num',1)->update();
            }
            return JsonService::success('修改成功');
        }
    }

    //购物车直接输入数量
    public function updateCartGoodsNum(){
        if($this->request->isAjax()){
            $cart_id = $this->request->post('cart_id');
            $num = $this->request->post('num',0);
            if($num < 1){
                return JsonService::error('数量不能小于1');
            }

            $cartGoods = CartModel::where(['id'=>$cart_id,'user_id'=>$this->member_id])->with(['getGoods'])->find();
            //判断是否是规格产品
            if($cartGoods['spec_price_goods_id'] > 0){
                $specGoods = SpecGoodsPrice::getOne(['item_id'=>$cartGoods['spec_price_goods_id'],'goods_id'=>$cartGoods['goods_id'],'key'=>$cartGoods['spec_key']],'store_count');
                if($num  > $specGoods['store_count']){
                    return JsonService::error('数量超出库存数量');
                }
            }else{
                if($num  > $cartGoods['getGoods']['store_count']){
                    return JsonService::error('数量超出库存数量');
                }
            }

            CartModel::updateOne(['goods_num'=>$num],['id'=>$cart_id,'user_id'=>$this->member_id]);
            return JsonService::success('修改成功');
        }
    }

    //删除购物车
    public function deleteCartGoods(){
        if($this->request->isAjax()){
            $cart_id = $this->request->post('cart_id');
            CartModel::delByWhere(['id'=>$cart_id,'user_id'=>$this->member_id]);
            return JsonService::success('删除成功');
        }
    }
}